﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using Assist;
using Autodesk.Revit.DB;
using DataBase;

namespace YJKDataLookAfter
{
	public class DbProject : DbObject
	{
		[Editor(typeof(FileNameEditor), typeof(UITypeEditor))]
		[Category("属性")]
		[Description("建筑模型")]
		[DisplayName("建筑模型")]
		public string RacFilePath
		{
			get
			{
				return this.mRacFilePath;
			}
			set
			{
				this.mRacFilePath = value;
			}
		}

		[Editor(typeof(FileNameEditor), typeof(UITypeEditor))]
		[Category("属性")]
		[Description("结构模型")]
		[DisplayName("结构模型")]
		public string RstFilePath
		{
			get
			{
				return this.mRstFilePath;
			}
			set
			{
				this.mRstFilePath = value;
			}
		}

		[Editor(typeof(FileNameEditor), typeof(UITypeEditor))]
		[Category("属性")]
		[Description("水暖电模型")]
		[DisplayName("水暖电模型")]
		public string RmeFilePath
		{
			get
			{
				return this.mRmeFilePath;
			}
			set
			{
				this.mRmeFilePath = value;
			}
		}

		[Category("属性")]
		[Description("项目名称")]
		[DisplayName("名称")]
		public string Name
		{
			get
			{
				return this.mName;
			}
			set
			{
				this.mName = value;
			}
		}

		[Category("属性")]
		[Description("项目信息")]
		[DisplayName("信息")]
		public string Infomation
		{
			get
			{
				return this.mInfomation;
			}
			set
			{
				this.mInfomation = value;
			}
		}

		public DbProject(string name, string infomation, string racPath, string rstPath, string rmePath)
		{
			this.mName = name;
			this.mInfomation = infomation;
			this.mRacFilePath = racPath;
			this.mRstFilePath = rstPath;
			this.mRmeFilePath = rmePath;
		}

		public List<DbUser> GetAllUser()
		{
			List<DbUser> list = new List<DbUser>();
			try
			{
				if (base.Database.Connection.State == ConnectionState.Closed)
				{
					base.Database.Connection.Open();
				}
				string sql = string.Format("SELECT UserId FROM tbProjectUserRelation WHERE ProjectId={0}", base.Id);
				DataTable dataTable = base.Database.ExecQuery(sql);
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
				if (dataTable == null)
				{
					return null;
				}
				for (int i = 0; i < dataTable.Rows.Count; i++)
				{
					int id = Convert.ToInt32(dataTable.Rows[i]["UserId"]);
					list.Add(DbUser.GetFromDB(id, BimServerDB.Default));
				}
			}
			catch (Exception ex)
			{
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
				throw ex;
			}
			return list;
		}

		public List<DbUser> GetAllArchEngineer()
		{
			List<DbUser> list = new List<DbUser>();
			try
			{
				if (base.Database.Connection.State == ConnectionState.Closed)
				{
					base.Database.Connection.Open();
				}
				string sql = string.Format("SELECT UserId FROM tbProjectUserRelation WHERE ProjectId={0} AND UserType={1}", base.Id, 0);
				DataTable dataTable = base.Database.ExecQuery(sql);
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
				if (dataTable == null)
				{
					return null;
				}
				for (int i = 0; i < dataTable.Rows.Count; i++)
				{
					int id = Convert.ToInt32(dataTable.Rows[i]["UserId"]);
					list.Add(DbUser.GetFromDB(id, BimServerDB.Default));
				}
			}
			catch (Exception ex)
			{
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
				throw ex;
			}
			return list;
		}

		public List<DbUser> GetAllMEPEngineer()
		{
			List<DbUser> list = new List<DbUser>();
			try
			{
				if (base.Database.Connection.State == ConnectionState.Closed)
				{
					base.Database.Connection.Open();
				}
				string sql = string.Format("SELECT UserId FROM tbProjectUserRelation WHERE ProjectId={0} AND UserType={1}", base.Id, 1);
				DataTable dataTable = base.Database.ExecQuery(sql);
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
				if (dataTable == null)
				{
					if (base.Database.Connection.State == ConnectionState.Open)
					{
						base.Database.Connection.Close();
					}
					return null;
				}
				for (int i = 0; i < dataTable.Rows.Count; i++)
				{
					int id = Convert.ToInt32(dataTable.Rows[i]["UserId"]);
					list.Add(DbUser.GetFromDB(id, BimServerDB.Default));
				}
			}
			catch (Exception ex)
			{
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
				throw ex;
			}
			return list;
		}

		public List<DbUser> GetAllStructureEngineer()
		{
			List<DbUser> list = new List<DbUser>();
			try
			{
				if (base.Database.Connection.State == ConnectionState.Closed)
				{
					base.Database.Connection.Open();
				}
				string sql = string.Format("SELECT UserId FROM tbProjectUserRelation WHERE ProjectId={0} AND UserType={1}", base.Id, 2);
				DataTable dataTable = base.Database.ExecQuery(sql);
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
				if (dataTable == null)
				{
					return null;
				}
				for (int i = 0; i < dataTable.Rows.Count; i++)
				{
					int id = Convert.ToInt32(dataTable.Rows[i]["UserId"]);
					list.Add(DbUser.GetFromDB(id, BimServerDB.Default));
				}
			}
			catch (Exception ex)
			{
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
				throw ex;
			}
			return list;
		}

		public static DbProject GetProjectByCurrentDoc(Autodesk.Revit.DB.Document doc)
		{
			DbProject dbProject = null;
			try
			{
				string text;
				if (RevitVersionFuncs.GetWorksharingCentralFilename(doc) != "")
				{
					text = RevitVersionFuncs.GetWorksharingCentralFilename(doc);
				}
				else
				{
					text = doc.PathName;
				}
				if (text != "")
				{
					BimServerDB @default = BimServerDB.Default;
					dbProject = DbProject.GetFromDbByRacPath(text, @default);
					if (dbProject == null)
					{
						dbProject = DbProject.GetFromDbByRmePath(text, @default);
					}
					if (dbProject == null)
					{
						dbProject = DbProject.GetFromDbByRstPath(text, @default);
					}
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
			return dbProject;
		}

		public List<DbElementForRemove> ElementForRemove()
		{
			return DbElementForRemove.GetByProject(this);
		}

		public List<DbSlab> SlabToModify()
		{
			return DbSlab.GetByProject(this);
		}

		public List<DbBeam> BeamToModify()
		{
			return DbBeam.GetByProject(this);
		}

		public List<DbColumn> ColumnToModify()
		{
			return DbColumn.GetByProject(this);
		}

		public List<DbDitch> DitchToModify()
		{
			return DbDitch.GetByProject(this);
		}

		public List<DbOpening> OpeningToModify()
		{
			return DbOpening.GetByProject(this);
		}

		public List<DbWall> WallToModify()
		{
			return DbWall.GetByProject(this);
		}

		public static DbProject GetFromDB(int id, IDatabase db)
		{
			DbProject result;
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				string sql = string.Format("SELECT Name,Infomation, RacPath,RstPath,RmePath FROM tbproject WHERE Id={0}", id);
				DataTable dataTable = db.ExecQuery(sql);
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				if (dataTable == null)
				{
					result = null;
				}
				else
				{
					string name = Convert.ToString(dataTable.Rows[0]["Name"]);
					string infomation = Convert.ToString(dataTable.Rows[0]["Infomation"]);
					string racPath = Convert.ToString(dataTable.Rows[0]["RacPath"]);
					string rstPath = Convert.ToString(dataTable.Rows[0]["RstPath"]);
					string rmePath = Convert.ToString(dataTable.Rows[0]["RmePath"]);
					result = new DbProject(name, infomation, racPath, rstPath, rmePath)
					{
						mId = id,
						mDatabase = db
					};
				}
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public static DbProject GetFromDbByRacPath(string racPath, IDatabase db)
		{
			DbProject result;
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				string sql = string.Format("SELECT Id,Name,Infomation, RacPath,RstPath,RmePath FROM tbproject WHERE RacPath='{0}'", DbProject.WritePathString(racPath));
				DataTable dataTable = db.ExecQuery(sql);
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				if (dataTable == null || dataTable.Rows.Count <= 0)
				{
					result = null;
				}
				else
				{
					int mId = Convert.ToInt32(dataTable.Rows[0]["Id"]);
					string name = Convert.ToString(dataTable.Rows[0]["Name"]);
					string infomation = Convert.ToString(dataTable.Rows[0]["Infomation"]);
					string rstPath = Convert.ToString(dataTable.Rows[0]["RstPath"]);
					string rmePath = Convert.ToString(dataTable.Rows[0]["RmePath"]);
					result = new DbProject(name, infomation, racPath, rstPath, rmePath)
					{
						mId = mId,
						mDatabase = db
					};
				}
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public static DbProject GetFromDbByRstPath(string rstPath, IDatabase db)
		{
			DbProject result;
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				string sql = string.Format("SELECT Id,Name,Infomation, RacPath,RstPath,RmePath FROM tbproject WHERE RstPath='{0}'", DbProject.WritePathString(rstPath));
				DataTable dataTable = db.ExecQuery(sql);
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				if (dataTable == null || dataTable.Rows.Count <= 0)
				{
					result = null;
				}
				else
				{
					int mId = Convert.ToInt32(dataTable.Rows[0]["Id"]);
					string name = Convert.ToString(dataTable.Rows[0]["Name"]);
					string infomation = Convert.ToString(dataTable.Rows[0]["Infomation"]);
					string racPath = Convert.ToString(dataTable.Rows[0]["RacPath"]);
					string rmePath = Convert.ToString(dataTable.Rows[0]["RmePath"]);
					result = new DbProject(name, infomation, racPath, rstPath, rmePath)
					{
						mId = mId,
						mDatabase = db
					};
				}
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public static DbProject GetFromDbByRmePath(string rmePath, IDatabase db)
		{
			DbProject result;
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				string sql = string.Format("SELECT Id,Name,Infomation, RacPath,RstPath,RmePath FROM tbproject WHERE RmePath='{0}'", DbProject.WritePathString(rmePath));
				DataTable dataTable = db.ExecQuery(sql);
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				if (dataTable == null || dataTable.Rows.Count <= 0)
				{
					result = null;
				}
				else
				{
					int mId = Convert.ToInt32(dataTable.Rows[0]["Id"]);
					string name = Convert.ToString(dataTable.Rows[0]["Name"]);
					string infomation = Convert.ToString(dataTable.Rows[0]["Infomation"]);
					string racPath = Convert.ToString(dataTable.Rows[0]["RacPath"]);
					string rstPath = Convert.ToString(dataTable.Rows[0]["RstPath"]);
					result = new DbProject(name, infomation, racPath, rstPath, rmePath)
					{
						mId = mId,
						mDatabase = db
					};
				}
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public static List<DbProject> GetFromDB(IDatabase db)
		{
			List<DbProject> result;
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				List<DbProject> list = new List<DbProject>();
				string sql = string.Format("SELECT Id,Name,Infomation, RacPath,RstPath,RmePath FROM tbproject", Array.Empty<object>());
				DataTable dataTable = db.ExecQuery(sql);
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				if (dataTable == null)
				{
					result = null;
				}
				else
				{
					for (int i = 0; i < dataTable.Rows.Count; i++)
					{
						int mId = Convert.ToInt32(dataTable.Rows[i]["Id"]);
						string name = Convert.ToString(dataTable.Rows[i]["Name"]);
						string infomation = Convert.ToString(dataTable.Rows[i]["Infomation"]);
						string racPath = Convert.ToString(dataTable.Rows[i]["RacPath"]);
						string rstPath = Convert.ToString(dataTable.Rows[i]["RstPath"]);
						string rmePath = Convert.ToString(dataTable.Rows[i]["RmePath"]);
						list.Add(new DbProject(name, infomation, racPath, rstPath, rmePath)
						{
							mId = mId,
							mDatabase = db
						});
					}
					result = list;
				}
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public static string WritePathString(string path)
		{
			return path.Replace("\\\\", "\\");
		}

		public override void AddToDb(IDatabase db)
		{
			try
			{
				string text = "";
				string text2 = "";
				string text3 = "";
				if (this.RacFilePath != null)
				{
					text = DbProject.WritePathString(this.RacFilePath);
				}
				if (this.RstFilePath != null)
				{
					text2 = DbProject.WritePathString(this.RstFilePath);
				}
				if (this.RmeFilePath != null)
				{
					text3 = DbProject.WritePathString(this.RmeFilePath);
				}
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				string sql = string.Format("INSERT INTO tbproject (Name,Infomation, RacPath,RstPath,RmePath) VALUES ('{0}','{1}','{2}','{3}','{4}')", new object[]
				{
					this.mName,
					this.mInfomation,
					text,
					text2,
					text3
				});
				db.ExecuteCommand(sql);
				string sql2 = string.Format("select max(Id) from tbproject", Array.Empty<object>());
				DataTable dataTable = db.ExecQuery(sql2);
				this.mId = Convert.ToInt32(dataTable.Rows[0][0]);
				this.mDatabase = db;
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
		}

		public override void UpdateToDb()
		{
			try
			{
				string text = "";
				string text2 = "";
				string text3 = "";
				if (this.RacFilePath != null)
				{
					text = DbProject.WritePathString(this.RacFilePath);
				}
				if (this.RstFilePath != null)
				{
					text2 = DbProject.WritePathString(this.RstFilePath);
				}
				if (this.RmeFilePath != null)
				{
					text3 = DbProject.WritePathString(this.RmeFilePath);
				}
				if (base.Database.Connection.State == ConnectionState.Closed)
				{
					base.Database.Connection.Open();
				}
				string sql = string.Format("UPDATE tbproject SET Name = '{0}',Infomation='{1}',RacPath='{2}',RstPath='{3}',RmePath='{4}' WHERE Id ='{5}'", new object[]
				{
					this.mName,
					this.mInfomation,
					text,
					text2,
					text3,
					base.Id
				});
				base.Database.ExecuteCommand(sql);
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
				throw ex;
			}
		}

		public override void RemoveFromDb()
		{
			try
			{
				if (base.Database.Connection.State == ConnectionState.Closed)
				{
					base.Database.Connection.Open();
				}
				string sql = string.Format("DELETE FROM tbproject WHERE Id = {0}", base.Id);
				base.Database.ExecuteCommand(sql);
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
				foreach (DbElementForRemove dbElementForRemove in this.ElementForRemove())
				{
					dbElementForRemove.RemoveFromDb();
				}
				foreach (DbSlab dbSlab in this.SlabToModify())
				{
					dbSlab.RemoveFromDb();
				}
				foreach (DbBeam dbBeam in this.BeamToModify())
				{
					dbBeam.RemoveFromDb();
				}
				foreach (DbColumn dbColumn in this.ColumnToModify())
				{
					dbColumn.RemoveFromDb();
				}
				foreach (DbDitch dbDitch in this.DitchToModify())
				{
					dbDitch.RemoveFromDb();
				}
				foreach (DbOpening dbOpening in this.OpeningToModify())
				{
					dbOpening.RemoveFromDb();
				}
				foreach (DbWall dbWall in this.WallToModify())
				{
					dbWall.RemoveFromDb();
				}
			}
			catch (Exception ex)
			{
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
				throw ex;
			}
		}

		public override string ToString()
		{
			return this.mName;
		}

		private string mRacFilePath = "";

		private string mRstFilePath = "";

		private string mRmeFilePath = "";

		private string mName;

		private string mInfomation;
	}
}
